package com.google.code.bankagregator.dao;

import com.google.code.bankagregator.entities.Bank;

import javax.ejb.Stateless;
import java.util.List;

/**
 * Bank CrudDAO class
 * User: kaiser
 */

@Stateless
public class BankDAO extends DAO{

    public void update(Bank entity) {
        getEntityManager().merge(entity);
    }

    public void delete(Bank entity) {
        Bank deletedApplicant = entity;
        if (!getEntityManager().contains(entity)) {
            deletedApplicant = getEntityManager().merge(entity);
        }

        getEntityManager().remove(deletedApplicant);
    }

    public void delete(int id) {
        Bank bank = getById(id);
        delete(bank);
    }

    public void create(Bank entity) {
        getEntityManager().persist(entity);
    }

    public Bank getById(int id) {
        return getEntityManager().find(Bank.class, id);
    }

    public List<Bank> getAll() {
        return getEntityManager().createQuery("SELECT b FROM Bank b").getResultList();
    }
}
